package com.xiaoxin.experience.service.tree;

import com.xiaoxin.experience.common.RestResponse;
import com.xiaoxin.experience.service.tree.common.JsTree;
import com.xiaoxin.experience.service.tree.model.Api;
import com.xiaoxin.experience.service.tree.model.ApiDirectory;
import com.xiaoxin.experience.service.tree.model.ApiTree;
import com.xiaoxin.experience.service.tree.storage.ApiJsonFileHandler;
import com.xiaoxin.experience.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author xiaoxin
 */
@Slf4j
@Service
public class ApiTreeService
{

    public List<JsTree> selectApiTree()
    {
        List<ApiDirectory> apiDirectories = ApiJsonFileHandler.getApiDirectories();
        if (apiDirectories.isEmpty())
        {
            return new ArrayList<>();
        }
        List<Api> apis = ApiJsonFileHandler.getApis();
        ApiTree apiTree = new ApiTree(apiDirectories);
        apiTree.decorateLeaves(apis);
        JsTree jsTree = apiTree.getTreeRoot().buildJsTree(ApiDirectory::toTreeNode, Api::toTreeNode);
        return jsTree.getChildren();
    }

    public void createDirectory(ApiDirectory apiDirectory)
    {
        apiDirectory.setId(ApiJsonFileHandler.generateDirId());
        ApiJsonFileHandler.getApiDirectories().add(apiDirectory);
        ApiJsonFileHandler.saveApiDirectory();
    }

    public RestResponse<Void> deleteDirectory(String id)
    {
        boolean deleteResult = ApiJsonFileHandler.deleteApiDirectory(id);
        if(deleteResult)
        {
            return RestResponse.success();
        }

        return RestResponse.fail("delete dir fail, the dir has sub-dir or api");
    }

    public void createApi(Api api)
    {
        api.setId(ApiJsonFileHandler.generateApiId());
        ApiJsonFileHandler.getApis().add(api);
        ApiJsonFileHandler.saveApi();
    }

    public void updateApi(Api api)
    {
        if (Objects.isNull(api))
        {
            return;
        }
        String id = api.getId();
        if (StringUtil.isBlank(id))
        {
            return;
        }
        List<Api> apis = ApiJsonFileHandler.getApis();
        for (Api a : apis)
        {
            if (api.getId().equals(a.getId()))
            {
                a.setRequestMethod(api.getRequestMethod());
                a.setRequestUrl(api.getRequestUrl());
                a.setRequestBody(api.getRequestBody());
                a.setRequestHeader(api.getRequestHeader());
                a.setRequestPathParam(api.getRequestPathParam());
                a.setRequestParam(api.getRequestParam());
                a.setRequestAuth(api.getRequestAuth());
                a.setPassword(api.getPassword());
                a.setUsername(api.getUsername());
                a.setScript(api.getScript());
                ApiJsonFileHandler.saveApi();
                return;
            }
        }

    }

    public void deleteApi(String id)
    {
        ApiJsonFileHandler.deleteApi(id);
    }
}
